#!/usr/bin/env ruby -w

=begin
Copyright (c) 2007 RubyMSN team

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
=end

require 'msn/msn'

if ARGV.length != 2
	puts "Usage: #{$0} <email_address> <password>"
	exit
end

email = ARGV[0]
password = ARGV[1]
puts "Signing in... please wait."

# create a new MSN connection
msnsock = MSNConnection.new(email, password)

# connect events
msnsock.signed_in = lambda { puts "Signed in"
	msnsock.change_nickname "Rob42 -- Don't panic!"
}

# display updated buddy, show full contact list
msnsock.buddy_update = lambda {|oldcontact, contact|
	puts "Updated #{contact.email} (status: #{oldcontact.status.name} -> #{contact.status.name})"

	puts "Contact list:"
	msnsock.contactlists["FL"].list.each {|email, contact|
		puts " - #{email}: #{contact.status.name}" if contact.status.name != "Offline"
	}
}

msnsock.new_chat_session = lambda {|tag, session|
	puts "New chat session request. Tag: #{tag}"
	# chat sessions have events too!
	session.message_received = lambda {|sender, message|
		puts sender + " says: " + message
		# just for fun ;-) The possibilities are endless...
		case message
		when /Ping\?\ \[msgplus\]/
			session.say "I'm sorry, I don't understand this command yet!" #TODO generate response on this
		when /^ping/i
			message = message.gsub(/i/, "o").gsub(/\?/, "!")
			session.say message
		when /^42$/
			session.say "You are well educated. You know the answer to Life, the Universe and Everything :-)\nBut do you know the Question?"
		when /^42[: ]+quit$/
			session.say "Bye, bye now!"
			exit
		when /^!help/i
			session.say "Supported commands:\n- !help\n- !info\n- !ping\n- !h2g2"
		when /^!info/i
			session.say "More info @ http://code.google.com/p/rubymsn"
		when /^!h2g2/i
			session.say "You want more information about \"The Hitchhiker's Guide To The Galaxy\"?"
			session.say "The book: "
			session.say "The TV series (1981): http://www.imdb.com/title/tt0081874/"
			session.say "The movie (2005): http://www.imdb.com/title/tt0371724/"
		when /^!ping/i
			session.say "Ping? [msgplus]" #invokes same thing as /ping of Messenger Plus! 
		when /^!/
			session.say "Unsupported command"
		else
			session.say message.reverse
		end
	}
	session.session_started = lambda {
		puts "Session with tag '" + tag + "' started!"
		session.say "Type !help for possible commands"
		session.say "By the way: shouldn't you be working on something more important? Like the destruction of planet Earth!"
	}
	session.participants_updated = lambda {
		puts "Participants in '#{tag}': " + session.participants.list.to_s
	}
	
	session.start
}

# start signing in!
msnsock.start

while true
	sleep 1
end

